package com.x.provider.customer.service.customer;

import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.x.core.utils.BeanUtil;
import com.x.core.utils.MyBatisPlusUtil;
import com.x.provider.api.customer.model.dto.customer.CustomerResumePreferenceSaveReqDTO;
import com.x.provider.customer.mapper.CustomerResumePreferenceMapper;
import com.x.provider.customer.model.domain.customer.CustomerResumePreference;
import com.x.provider.customer.model.query.customer.CustomerResumePreferenceQuery;
import com.x.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerResumePreferenceServiceImpl implements CustomerResumePreferenceService {

    private final CustomerResumePreferenceMapper customerResumePreferenceMapper;

    public CustomerResumePreferenceServiceImpl(CustomerResumePreferenceMapper customerResumePreferenceMapper){
        this.customerResumePreferenceMapper = customerResumePreferenceMapper;
    }

    @Override
    public void save(CustomerResumePreferenceSaveReqDTO customerPreference) {
        CustomerResumePreference entity = getCustomerPreference(CustomerResumePreferenceQuery.builder().customerId(customerPreference.getCustomerId()).build());
        if (entity == null){
            entity = new CustomerResumePreference();
        }
        BeanUtil.prepare(customerPreference, entity);
        entity.setInDegreeList(StringUtil.toString(customerPreference.getInDegreeList()));
        entity.setInMarriageStatusList(StringUtil.toString(customerPreference.getInMarriageStatusList()));
        entity.setInRegionIdLevel2List(StringUtil.toString(customerPreference.getInRegionIdLevel2List()));
        MyBatisPlusUtil.save(customerResumePreferenceMapper, entity);
    }

    @Override
    public List<CustomerResumePreference> list(CustomerResumePreferenceQuery query) {
        return listCustomerPreference(query);
    }

    public List<CustomerResumePreference> listCustomerPreference(CustomerResumePreferenceQuery query){
        return customerResumePreferenceMapper.selectList(build(query));
    }

    public CustomerResumePreference getCustomerPreference(CustomerResumePreferenceQuery query){
        return customerResumePreferenceMapper.selectOne(build(query));
    }

    private LambdaQueryWrapper<CustomerResumePreference> build(CustomerResumePreferenceQuery query){
        LambdaQueryWrapper<CustomerResumePreference> queryWrapper = new LambdaQueryWrapper<>();
        if (query.getCustomerId() != null){
            queryWrapper.eq(CustomerResumePreference::getCustomerId, query.getCustomerId());
        }
        if (query.getId() != null){
            queryWrapper.eq(CustomerResumePreference::getId, query.getId());
        }
        if (query.getCustomerIdList() != null){
            queryWrapper.in(CustomerResumePreference::getCustomerId, query.getCustomerIdList());
        }
        return queryWrapper;
    }

}
